package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.widgets.ItemListView;

/**
 * Created by Alan on 2018/9/13.
 */

public class GameListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private Map<String, String> downTemp = new HashMap<>();
    private boolean isHasOne = false;

    public GameListAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size() > 0 && downTemp.containsKey(gameId)) {
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == ViewType.TYPE_ITEM) {
            GameListBaseBean list = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return list;
        }
        return null;
    }

    public enum ViewType {
        TYPE_ITEM
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_ITEM.ordinal()) {
            holder = new LibraryThreeHolder(inflater.inflate(R.layout.item_gamelib_three_list_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof LibraryThreeHolder) {
            LibraryThreeHolder libraryTwoHolder = (LibraryThreeHolder) holder;
            GameListBaseBean listTemp = (GameListBaseBean) viewBean.getObj();

            if (position<3) {
                libraryTwoHolder.itemGameLibRankingTopTv.setText(""+(position+1));
                libraryTwoHolder.itemGameLibRankingTopTv.setVisibility(View.VISIBLE);
                libraryTwoHolder.itemGameLibRankingTv.setVisibility(View.GONE);
            } else {
                libraryTwoHolder.itemGameLibRankingTv.setText(""+(position+1));
                libraryTwoHolder.itemGameLibRankingTv.setVisibility(View.VISIBLE);
                libraryTwoHolder.itemGameLibRankingTopTv.setVisibility(View.GONE);
            }
            downTemp.put(listTemp.getGame_id(), position + "");
            libraryTwoHolder.listItem.hitLine();
            libraryTwoHolder.listItem.setData(mContext, listTemp);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    private class MyListener implements View.OnClickListener {

        private Activity context;
        private JumpBaseBean tempBean;

        public MyListener(Activity context, JumpBaseBean temp) {
            this.context = context;
            this.tempBean = temp;
        }

        @Override
        public void onClick(View v) {
            IntentUtil.getInstance().intentTo(context, tempBean);
        }
    }

    public class LibraryThreeHolder extends RecyclerView.ViewHolder {
        ItemListView listItem;
        TextView itemGameLibRankingTopTv;
        TextView itemGameLibRankingTv;

        public LibraryThreeHolder(View view) {
            super(view);
            listItem = view.findViewById(R.id.listItem);
            itemGameLibRankingTopTv = view.findViewById(R.id.itemGameLibRankingTopTv);
            itemGameLibRankingTv = view.findViewById(R.id.itemGameLibRankingTv);
        }
    }
}
